package com.mutual.help.controller;

import com.mutual.help.common.PageResult;
import com.mutual.help.entity.Picture;
import com.mutual.help.entity.Question;
import com.mutual.help.entity.Type;
import com.mutual.help.service.PictureService;
import com.mutual.help.utils.WeChatResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class PictureController {

    @Autowired
    private PictureService pictureService;

    //页面跳转
    @RequestMapping("/picture/toPage")
    public String toPage(){
        return "picture/list";
    }

    //页面跳转
    @RequestMapping("/picture/toAddPage")
    public String toAddPage(){
        return "picture/add";
    }

    //分页查询所有图片信息
    @RequestMapping("/picture/query")
    @ResponseBody
    public PageResult<Picture> list(Integer page, Integer limit) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 10;
        }
        List<Picture> allArticle = pictureService.getAllPicture(page, limit);
        Integer count = pictureService.countData();
        return new PageResult<>(allArticle, count);
    }

    @RequestMapping("/picture/insert")
    @ResponseBody
    public PageResult add(Picture type){
        if(pictureService.save(type)){
            return new PageResult("新增成功",200);
        }
        return new PageResult("新增失败",400);
    }

    @RequestMapping("/picture/delete/{id}")
    @ResponseBody
    public PageResult delete(@PathVariable("id") Integer id){
        if(pictureService.removeById(id)){
            return new PageResult("删除成功",200);
        }
        return new PageResult("删除失败",400);
    }

    @RequestMapping("wx/getAllPicture")
    @ResponseBody
    public Object getAllPicture(){
        List<Picture> list = pictureService.list();
        return WeChatResponseUtil.ok(list);
    }
}
